package com.xx.common.utils.file;

import com.xx.common.utils.ServletUtils;
import com.xx.common.utils.spring.SpringUtils;
import io.minio.*;
import io.minio.http.Method;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;

/**
 * Minio 文件存储工具类
 *
 * @author xx
 */
@Slf4j
public class MinioUtil {
    /**
     * 上传文件
     *
     * @param bucketName 桶名称
     * @param fileName
     * @throws IOException
     */
    public static String uploadFile(String bucketName, String fileName, MultipartFile multipartFile) throws IOException {
        String url = "";
        MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
        try (InputStream inputStream = multipartFile.getInputStream()) {
            minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileName).stream(inputStream, multipartFile.getSize(), -1).contentType(multipartFile.getContentType()).build());
            url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(fileName).method(Method.GET).build());
            url = url.substring(0, url.indexOf('?'));
            return ServletUtils.urlDecode(url);
        } catch (Exception e) {
            throw new IOException(e.getMessage(), e);
        }
    }

    /**
     * 获取文件外链
     *
     * @param bucketName 桶名称
     * @param objectName 文件名称
     * @return url
     */
    public static String getObjectURL(String bucketName, String objectName) throws Exception {
        MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
        GetPresignedObjectUrlArgs build = GetPresignedObjectUrlArgs.builder()
                .method(Method.GET)
                .bucket(bucketName)
                .object(objectName)
                .expiry(3, TimeUnit.MINUTES)
                .build();
        return minioClient.getPresignedObjectUrl(build);
    }

    /**
     * 下载文件
     *
     * @param bucketName 桶名称
     * @param objectName 文件名称
     * @return
     * @throws Exception
     */
    public InputStream downloadFile(String bucketName, String objectName) throws Exception {
        MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
        return minioClient.getObject(GetObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .build());
    }

    /**
     * 删除文件
     *
     * @param bucketName 桶名称
     * @param objectName 文件名称
     * @throws Exception
     */
    public void deleteFile(String bucketName, String objectName) throws Exception {
        MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
        minioClient.removeObject(RemoveObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .build());
    }

    /**
     * 获取文件访问地址
     *
     * @param prefixUrl 路径
     * @param fileName  文件名称
     * @throws Exception
     */
    public String getPreviewFileUrl(String prefixUrl, String fileName) {
        String url = "";
        try {
            url = prefixUrl + "/" + fileName;
        } catch (Exception e) {
            log.error("获取文件访问地址失败：" + url, e);
        }
        return url;
    }

}
